﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Drawing.Design;
using System.Collections;

namespace comm.objst
{
    #region DObjEV
    [Serializable]
    public class DObjEV
    {
        private Dictionary<string, lib.ObjEV> _evDic = null;
        public Dictionary<string, lib.ObjEV> EVDic
        {
            get
            {
                if (_evDic == null)
                {
                    _evDic = new Dictionary<string, lib.ObjEV>();
                }
                return _evDic;
            }
            set { _evDic = value; }
        }
    }
    #endregion

    #region DMainJS
    [Serializable]
    public class DMainJS
    {
        public const string FILE_NAME = "main.evdic";

        private string _designSearchKey = null;
        public string DesignSearchKey
        {
            get
            {
                if (_designSearchKey == null)
                {
                    _designSearchKey = "";
                }
                return _designSearchKey;
            }
            set { _designSearchKey = value; }
        }

        private string _designSelKey = null;
        public string DesignSelKey
        {
            get
            {
                if (_designSelKey == null)
                {
                    _designSelKey = "";
                }
                return _designSelKey;
            }
            set { _designSelKey = value; }
        }

        private Dictionary<string, bool> _designTreeItemExpandDic = null;
        public Dictionary<string, bool> DesignTreeItemExpandDic
        {
            get
            {
                if (_designTreeItemExpandDic == null)
                {
                    _designTreeItemExpandDic = new Dictionary<string, bool>();
                }
                return _designTreeItemExpandDic;
            }
            set { _designTreeItemExpandDic = value; }
        }

        private Dictionary<string, DObjEV> _allObjEVDic = null;
        public Dictionary<string, DObjEV> AllObjEVDic
        {
            get
            {
                if (_allObjEVDic == null)
                {
                    _allObjEVDic = new Dictionary<string, DObjEV>();
                }
                return _allObjEVDic;
            }
            set { _allObjEVDic = value; }
        }

    }
    #endregion

    #region IClassBind
    public interface IClassBind
    {
        string ClassType { get; set; }
    }
    #endregion

    #region DPlan
    [Serializable]
    public abstract class DPlan
    {
        public const string DEFAULT_KEY = "DEFAULT";

        private string _name = null;
        [DisplayName("名称")]
        public string Name
        {
            get
            {
                if (_name == null)
                {
                    _name = "";
                }
                return _name;
            }
            set { _name = value; }
        }
    }
    #endregion

    #region IExistPlan
    public interface IExistPlan
    {
        IDictionary GetPlanDic();
    }
    #endregion

    #region IExistPlan
    public interface IExistPlan<T> : IExistPlan
    {
        Dictionary<string, T> PlanDic { get; set; }

        T GetPlan(string key);
    }
    #endregion

    #region DDefine
    [Serializable]
    public abstract class DDefine
    {
        private string _name = null;
        [DisplayName("名称")]
        public string Name
        {
            get
            {
                if (_name == null)
                {
                    _name = "";
                }
                return _name;
            }
            set { _name = value; }
        }

        private DateTime _createTime = DateTime.Now;
        [DisplayName("创建日期"), Category("开发"), ReadOnly(true)]
        public DateTime CreateTime
        {
            get { return _createTime; }
            set { _createTime = value; }
        }

        private string _version = null;
        [DisplayName("版本号"), Category("开发")]
        public string Version
        {
            get
            {
                if (_version == null)
                {
                    _version = "0";
                }
                return _version;
            }
            set { _version = value; }
        }

        private string _remark = null;
        [DisplayName("备注"), Category("开发"), Editor(typeof(editor.TextareaEditor), typeof(UITypeEditor))]
        public string Remark
        {
            get
            {
                if (_remark == null)
                {
                    _remark = "";
                }
                return _remark;
            }
            set { _remark = value; }
        }
    }
    #endregion

    #region DLanguage
    [Serializable]
    public class DLanguage : DDefine
    {
        private string _zh = null;
        [DisplayName("中文")]
        public string ZH
        {
            get
            {
                if (_zh == null)
                {
                    _zh = "";
                }
                return _zh;
            }
            set { _zh = value; }
        }

        private string _en = null;
        [DisplayName("英文")]
        public string EN
        {
            get
            {
                if (_en == null)
                {
                    _en = "";
                }
                return _en;
            }
            set { _en = value; }
        }
    }
    #endregion

    #region DNValue
    [Serializable]
    public class DNValue : DDefine
    {
        private NValueGroup _group = NValueGroup.ALL;
        [DisplayName("分组")]
        public NValueGroup Group
        {
            get { return _group; }
            set
            {
                comm.EnumHelper.CheckValueDefine<NValueGroup>(value);
                _group = value;
            }
        }

        private List<NValueBehaviorType> _behaviorTypeList = null;
        [DisplayName("行为列表"), Editor(typeof(editor.NValueBehaviorTypeListEditor), typeof(UITypeEditor))]
        public List<NValueBehaviorType> BehaviorTypeList
        {
            get
            {
                if (_behaviorTypeList == null)
                {
                    _behaviorTypeList = new List<NValueBehaviorType>();
                }
                return _behaviorTypeList;
            }
            set { _behaviorTypeList = value; }
        }

        [DisplayName("默认值")]
        public long Value { get; set; }

        private NValueType _type = NValueType.UNSIGNED;
        [DisplayName("类型")]
        public NValueType Type
        {
            get { return _type; }
            set
            {
                comm.EnumHelper.CheckValueDefine<NValueType>(value);
                _type = value;
            }
        }

        [DisplayName("持久化")]
        public bool Save { get; set; }
    }
    #endregion

    #region DState
    [Serializable]
    public class DState : DDefine, IClassBind
    {
        [DisplayName("持久化")]
        public bool Save { get; set; }

        private string _classType = null;
        [Editor(typeof(editor.ClassTypeEditor), typeof(UITypeEditor))]
        public string ClassType
        {
            get
            {
                if (_classType == null)
                {
                    _classType = "";
                }
                return _classType;
            }
            set { _classType = value; }
        }
    }
    #endregion

    #region DItemCategory
    [Serializable]
    public class DItemCategory : DDefine, ILinearTreeNode
    {
        private string _name = null;
        public new string Name
        {
            get
            {
                if (_name == null)
                {
                    _name = "";
                }
                return _name;
            }
            set { _name = value; }
        }

        private string _parent = null;
        public string Parent
        {
            get
            {
                if (_parent == null)
                {
                    _parent = "";
                }
                return _parent;
            }
            set { _parent = value; }
        }
    }
    #endregion

    #region DItemPlan
    [Serializable]
    public class DItemPlan : DPlan
    {
        private List<ItemEquipType> _soltEquipTypeList = null;
        [DisplayName("插槽列表"), Category("插槽"), Editor(typeof(editor.ItemEquipTypeListEditor), typeof(UITypeEditor))]
        public List<ItemEquipType> SoltEquipTypeList
        {
            get
            {
                if (_soltEquipTypeList == null)
                {
                    _soltEquipTypeList = new List<ItemEquipType>();
                }
                return _soltEquipTypeList;
            }
            set { _soltEquipTypeList = value; }
        }

        private Dictionary<string, long> _needNVValueDic = null;
        [DisplayName("NVValue(先决)"), Category("初始"), Editor(typeof(editor.NVValueDicEditor), typeof(UITypeEditor))]
        public Dictionary<string, long> NeedNVValueDic
        {
            get
            {
                if (_needNVValueDic == null)
                {
                    _needNVValueDic = new Dictionary<string, long>();
                }
                return _needNVValueDic;
            }
            set { _needNVValueDic = value; }
        }

        private Dictionary<string, string> _computingExprDic = null;
        [DisplayName("NVComputing"), Category("初始"), Editor(typeof(editor.ComputingExprDicEditor), typeof(UITypeEditor))]
        public Dictionary<string, string> ComputingExprDic
        {
            get
            {
                if (_computingExprDic == null)
                {
                    _computingExprDic = new Dictionary<string, string>();
                }
                return _computingExprDic;
            }
            set { _computingExprDic = value; }
        }
    }
    #endregion

    #region DItem
    [Serializable]
    public class DItem : DDefine, IClassBind, IExistPlan<DItemPlan>
    {
        public static bool IsParentEquipType(ItemEquipType type)
        {
            var num = (int)type;
            return num >= 100 && num % 100 == 0;
        }
        public static comm.objst.ItemEquipType[] GetSubEquipTypeList(ItemEquipType type)
        {
            var list = new List<ItemEquipType>();
            var num = (int)type;
            if (IsParentEquipType(type))
            {
                var names = comm.EnumHelper.GetNames(typeof(ItemEquipType));
                foreach (var name in names)
                {
                    var t = comm.EnumHelper.Parse<ItemEquipType>(name);
                    if ((int)t > num && (int)t < num + 100)
                    {
                        list.Add(t);
                    }
                }
            }
            return list.ToArray();
        }
        public static comm.objst.ItemEquipType? GetParentEquipType(ItemEquipType type)
        {
            var num = (int)type;
            var num2 = num / 100 * 100;
            if (num2 >= 100)
            {
                var t = (ItemEquipType)num2;
                if (Enum.IsDefined(typeof(ItemEquipType), t) && t != type)
                {
                    return t;
                }
            }
            return null;
        }
        public static comm.objst.ItemEquipType[] ExpandAllEquipType(ItemEquipType[] types)
        {
            var list = new List<comm.objst.ItemEquipType>();
            foreach (var type in types)
            {
                var isParent = IsParentEquipType(type);
                var subTypes = GetSubEquipTypeList(type);
                if (isParent)
                {
                    list.AddRange(subTypes);
                }
                else
                {
                    list.Add(type);
                }
            }
            return list.ToArray();
        }

        private string _categoryId = null;
        [DisplayName("分类Id"), Category("物品分类"), Editor(typeof(editor.ItemCateChooseEditor), typeof(UITypeEditor))]
        public string CategoryId
        {
            get
            {
                if (_categoryId == null)
                {
                    _categoryId = "";
                }
                return _categoryId;
            }
            set { _categoryId = value; }
        }

        private ItemEquipType _equipType = ItemEquipType.HERO_ALL;
        [DisplayName("装备类型")]
        public ItemEquipType EquipType
        {
            get { return _equipType; }
            set
            {
                comm.EnumHelper.CheckValueDefine<ItemEquipType>(value);
                _equipType = value;
            }
        }

        private bool _canBind = true;
        [DisplayName("可以绑定")]
        public bool CanBind
        {
            get { return _canBind; }
            set { _canBind = value; }
        }

        private bool _packItemOnly = true;
        [DisplayName("格子独占")]
        public bool PackItemOnly
        {
            get { return _packItemOnly; }
            set { _packItemOnly = value; }
        }

        private string _classType = null;
        [Editor(typeof(editor.ClassTypeEditor), typeof(UITypeEditor))]
        public string ClassType
        {
            get
            {
                if (_classType == null)
                {
                    _classType = "";
                }
                return _classType;
            }
            set { _classType = value; }
        }

        private Dictionary<string, DItemPlan> _planDic = null;
        [Browsable(false)]
        public Dictionary<string, DItemPlan> PlanDic
        {
            get
            {
                if (_planDic == null)
                {
                    _planDic = new Dictionary<string, DItemPlan>();
                    _planDic.Add(DPlan.DEFAULT_KEY, new DItemPlan { Name = DPlan.DEFAULT_KEY });
                }
                return _planDic;
            }
            set { _planDic = value; }
        }

        public IDictionary GetPlanDic()
        {
            return PlanDic;
        }

        public DItemPlan GetPlan(string key = DPlan.DEFAULT_KEY)
        {
            if (PlanDic.ContainsKey(key))
            {
                return PlanDic[key];
            }
            return PlanDic[DPlan.DEFAULT_KEY];
        }
    }
    #endregion

    #region DObject
    [Serializable]
    public class DObject : DDefine, IClassBind
    {
        #region Data
        [Serializable]
        public class Data
        {
            public const string FILE_EXT_NAME = ".obj2";

            public static string[] GetPointTypes()
            {
                return EnumHelper.GetNames(typeof(comm.objst.PointType));
            }
            public static string[] GetActionTypes()
            {
                return EnumHelper.GetNames(typeof(comm.objst.ObjActionType));
            }
            public static string[] GetDirTypes()
            {
                return EnumHelper.GetNames(typeof(comm.objst.DirType));
            }
            public static string[] GetObjTypes()
            {
                return comm.EnumHelper.GetNames(typeof(ObjType));
            }

            private Dictionary<string, ObjAction> _actionDic = null;
            public Dictionary<string, ObjAction> ActionDic
            {
                get
                {
                    if (_actionDic == null)
                    {
                        _actionDic = new Dictionary<string, ObjAction>();
                    }
                    return _actionDic;
                }
                set { _actionDic = value; }
            }
        }
        #endregion

        private ObjType _objType = ObjType.HERO;
        [DisplayName("类型")]
        public ObjType ObjType
        {
            get { return _objType; }
            set
            {
                comm.EnumHelper.CheckValueDefine<ObjType>(value);
                _objType = value;
            }
        }

        PathSizeType _pathSizeType = PathSizeType.PS_1X1;
        public PathSizeType PathSizeType
        {
            get { return _pathSizeType; }
            set
            {
                comm.EnumHelper.CheckValueDefine<PathSizeType>(value);
                _pathSizeType = value;
            }
        }

        private MoveType _moveType = MoveType.GROUND_BY_PATH;
        [DisplayName("移动类型")]
        public MoveType MoveType
        {
            get { return _moveType; }
            set { _moveType = value; }
        }

        public bool AStepBySprite { get; set; }

        private string _objFile = null;
        [DisplayName("Obj文件"), Category("文件引用"), Editor(typeof(editor.FileChooseEditor), typeof(UITypeEditor))]
        public string ObjFile
        {
            get
            {
                if (_objFile == null)
                {
                    _objFile = "";
                }
                return _objFile;
            }
            set { _objFile = value; }
        }

        private string _rpackFile = null;
        [DisplayName("RPack文件"), Category("文件引用"), Editor(typeof(editor.FileChooseEditor), typeof(UITypeEditor))]
        public string RPackFile
        {
            get
            {
                if (_rpackFile == null)
                {
                    _rpackFile = "";
                }
                return _rpackFile;
            }
            set { _rpackFile = value; }
        }

        private string _classType = null;
        [Editor(typeof(editor.ClassTypeEditor), typeof(UITypeEditor))]
        public string ClassType
        {
            get
            {
                if (_classType == null)
                {
                    _classType = "";
                }
                return _classType;
            }
            set { _classType = value; }
        }
    }
    #endregion

    #region IDefEquip
    public interface IDefEquip
    {
        List<ItemEquipType> CanEquipTypeList { get; set; }
        Dictionary<ItemEquipType, string> DefEquipDic { get; set; }
    }
    #endregion

    #region DHeroPlan
    [Serializable]
    public class DHeroPlan : DPlan
    {
        private Dictionary<string, long> _defNVValueDic = null;
        [DisplayName("NVValue"), Category("初始"), Editor(typeof(editor.NVValueDicEditor), typeof(UITypeEditor))]
        public Dictionary<string, long> DefNVValueDic
        {
            get
            {
                if (_defNVValueDic == null)
                {
                    _defNVValueDic = new Dictionary<string, long>();
                }
                return _defNVValueDic;
            }
            set { _defNVValueDic = value; }
        }
    }
    #endregion

    #region DHero
    [Serializable]
    public class DHero : DDefine, IDefEquip, IExistPlan<DHeroPlan>
    {
        [DisplayName("玩家可以创建")]
        public bool CanCreateByPlayer { get; set; }

        private List<CreateState> _defStateList = null;
        [DisplayName("状态"), Category("初始"), Editor(typeof(editor.CreateStateEditor), typeof(UITypeEditor))]
        public List<CreateState> DefStateList
        {
            get
            {
                if (_defStateList == null)
                {
                    _defStateList = new List<CreateState>();
                }
                return _defStateList;
            }
            set { _defStateList = value; }
        }

        private List<CreateUnit> _defUnitList = null;
        [DisplayName("单位"), Category("初始"), Editor(typeof(editor.CreateUnitEditor), typeof(UITypeEditor))]
        public List<CreateUnit> DefUnitList
        {
            get
            {
                if (_defUnitList == null)
                {
                    _defUnitList = new List<CreateUnit>();
                }
                return _defUnitList;
            }
            set { _defUnitList = value; }
        }

        private List<CreateItem> _defItemList = null;
        [DisplayName("物品"), Category("初始"), Editor(typeof(editor.CreateItemEditor), typeof(UITypeEditor))]
        public List<CreateItem> DefItemList
        {
            get
            {
                if (_defItemList == null)
                {
                    _defItemList = new List<CreateItem>();
                }
                return _defItemList;
            }
            set { _defItemList = value; }
        }

        private List<ItemEquipType> _canEduipTypeList = null;
        [DisplayName("可装备类型"), Editor(typeof(editor.DefEquipEditor), typeof(UITypeEditor))]
        public List<ItemEquipType> CanEquipTypeList
        {
            get
            {
                if (_canEduipTypeList == null)
                {
                    _canEduipTypeList = new List<ItemEquipType>();
                }
                return _canEduipTypeList;
            }
            set { _canEduipTypeList = value; }
        }

        private Dictionary<ItemEquipType, string> _defEquipDic = null;
        [Browsable(false)]
        public Dictionary<ItemEquipType, string> DefEquipDic
        {
            get
            {
                if (_defEquipDic == null)
                {
                    _defEquipDic = new Dictionary<ItemEquipType, string>();
                }
                return _defEquipDic;
            }
            set { _defEquipDic = value; }
        }

        private Dictionary<string, DHeroPlan> _planDic = null;
        [Browsable(false)]
        public Dictionary<string, DHeroPlan> PlanDic
        {
            get
            {
                if (_planDic == null)
                {
                    _planDic = new Dictionary<string, DHeroPlan>();
                    _planDic.Add(DPlan.DEFAULT_KEY, new DHeroPlan { Name = DPlan.DEFAULT_KEY });
                }
                return _planDic;
            }
            set { _planDic = value; }
        }

        public IDictionary GetPlanDic()
        {
            return PlanDic;
        }

        public DHeroPlan GetPlan(string key = DPlan.DEFAULT_KEY)
        {
            if (PlanDic.ContainsKey(key))
            {
                return PlanDic[key];
            }
            return PlanDic[DPlan.DEFAULT_KEY];
        }
    }
    #endregion

    #region DUnitPlan
    [Serializable]
    public class DUnitPlan : DPlan
    {
        private Dictionary<string, long> _defNVValueDic = null;
        [DisplayName("NVValue"), Category("初始"), Editor(typeof(editor.NVValueDicEditor), typeof(UITypeEditor))]
        public Dictionary<string, long> DefNVValueDic
        {
            get
            {
                if (_defNVValueDic == null)
                {
                    _defNVValueDic = new Dictionary<string, long>();
                }
                return _defNVValueDic;
            }
            set { _defNVValueDic = value; }
        }
    }
    #endregion

    #region DUnit
    [Serializable]
    public class DUnit : DDefine, IDefEquip, IExistPlan<DUnitPlan>
    {
        private UnitType _unitType = UnitType.INF;
        [DisplayName("兵种")]
        public UnitType UnitType
        {
            get { return _unitType; }
            set
            {
                comm.EnumHelper.CheckValueDefine<UnitType>(value);
                _unitType = value;
            }
        }

        private UnitHireType _hireType = UnitHireType.FACTORY_BUY;
        [DisplayName("获取类型")]
        public UnitHireType HireType
        {
            get { return _hireType; }
            set
            {
                comm.EnumHelper.CheckValueDefine<UnitHireType>(value);
                _hireType = value;
            }
        }

        private List<ItemEquipType> _canEduipTypeList = null;
        [DisplayName("可装备类型"), Editor(typeof(editor.DefEquipEditor), typeof(UITypeEditor))]
        public List<ItemEquipType> CanEquipTypeList
        {
            get
            {
                if (_canEduipTypeList == null)
                {
                    _canEduipTypeList = new List<ItemEquipType>();
                }
                return _canEduipTypeList;
            }
            set { _canEduipTypeList = value; }
        }

        private Dictionary<ItemEquipType, string> _defEquipDic = null;
        [Browsable(false)]
        public Dictionary<ItemEquipType, string> DefEquipDic
        {
            get
            {
                if (_defEquipDic == null)
                {
                    _defEquipDic = new Dictionary<ItemEquipType, string>();
                }
                return _defEquipDic;
            }
            set { _defEquipDic = value; }
        }

        private Dictionary<string, DUnitPlan> _planDic = null;
        [Browsable(false)]
        public Dictionary<string, DUnitPlan> PlanDic
        {
            get
            {
                if (_planDic == null)
                {
                    _planDic = new Dictionary<string, DUnitPlan>();
                    _planDic.Add(DPlan.DEFAULT_KEY, new DUnitPlan { Name = DPlan.DEFAULT_KEY });
                }
                return _planDic;
            }
            set { _planDic = value; }
        }

        public IDictionary GetPlanDic()
        {
            return PlanDic;
        }

        public DUnitPlan GetPlan(string key = DPlan.DEFAULT_KEY)
        {
            if (PlanDic.ContainsKey(key))
            {
                return PlanDic[key];
            }
            return PlanDic[DPlan.DEFAULT_KEY];
        }
    }
    #endregion

    #region DMonster
    [Serializable]
    public class DMonster : DDefine
    {
    }
    #endregion

    #region DUnitUpgrade
    [Serializable]
    public class DUnitUpgrade : DDefine
    {
        private List<UnitUpgrade> _upgradeList = null;
        [DisplayName("升级表"), Editor(typeof(editor.UnitUpgradeEditor), typeof(UITypeEditor))]
        public List<UnitUpgrade> UpgradeList
        {
            get
            {
                if (_upgradeList == null)
                {
                    _upgradeList = new List<UnitUpgrade>();
                }
                return _upgradeList;
            }
            set { _upgradeList = value; }
        }
    }
    #endregion

    #region DRank
    [Serializable]
    public class DRank : DDefine
    {
        private int[] _levels = null;
        [DisplayName("等级区间")]
        public int[] Levels
        {
            get
            {
                if (_levels == null)
                {
                    _levels = new int[] { 0, 0 };
                }
                return _levels;
            }
            set { _levels = value; }
        }

        private string _title = null;
        [DisplayName("称号")]
        public string Title
        {
            get
            {
                if (_title == null)
                {
                    _title = "";
                }
                return _title;
            }
            set { _title = value; }
        }
    }
    #endregion

    #region DMap
    [Serializable]
    public class DMap : DDefine, IClassBind
    {
        #region Data
        [Serializable]
        public class Data
        {
            [Serializable]
            public class ImageBlock
            {
                public int X { get; set; }
                public int Y { get; set; }
                public byte[] ImageBytes { get; set; }
            }

            #region Obj
            [Serializable]
            public abstract class Obj
            {
                private string _name = null;
                public string Name
                {
                    get
                    {
                        if (_name == null)
                        {
                            _name = "";
                        }
                        return _name;
                    }
                    set { _name = value; }
                }

                public int LocX { get; set; }
                public int LocY { get; set; }

                public bool IsExternal { get; set; }

                public abstract List<int[]> GetPathList(DefineFile defineFile);
            }
            #endregion

            #region Sprite
            [Serializable]
            public class Sprite : Obj
            {
                private string _rpackFile = null;
                public string RPackFile
                {
                    get
                    {
                        if (_rpackFile == null)
                        {
                            _rpackFile = "";
                        }
                        return _rpackFile;
                    }
                    set { _rpackFile = value; }
                }

                private string _spriteKey = null;
                public string SpriteKey
                {
                    get
                    {
                        if (_spriteKey == null)
                        {
                            _spriteKey = "";
                        }
                        return _spriteKey;
                    }
                    set { _spriteKey = value; }
                }

                private string _spriteGroupKey = null;
                public string SpriteGroupKey
                {
                    get
                    {
                        if (_spriteGroupKey == null)
                        {
                            _spriteGroupKey = comm.objst.RSprite.DEFAULT_GROUP_KEY;
                        }
                        return _spriteGroupKey;
                    }
                    set { _spriteGroupKey = value; }
                }

                public override List<int[]> GetPathList(DefineFile defineFile)
                {
                    var list = new List<int[]>();
                    try
                    {
                        var rpack = comm.objst.RPack.FromFile(RPackFile.Replace("[ROOT]", defineFile.Folder));
                        var sprite = rpack.GetResource<comm.objst.RSprite>(SpriteKey);
                        var group = sprite.GetGroup(SpriteGroupKey);
                        list = group.MergeAllPath();
                    }
                    catch (Exception)
                    {
                    }
                    return list;
                }
            }
            #endregion

            #region SObj
            [Serializable]
            public class SObj : Obj
            {
                private string _dObjectId = null;
                public string DObjectId
                {
                    get
                    {
                        if (_dObjectId == null)
                        {
                            _dObjectId = "";
                        }
                        return _dObjectId;
                    }
                    set { _dObjectId = value; }
                }

                private string _designAction = null;
                public string DesignAction
                {
                    get
                    {
                        if (_designAction == null)
                        {
                            _designAction = comm.objst.ObjActionType.IDLE.ToString();
                        }
                        return _designAction;
                    }
                    set { _designAction = value; }
                }

                private string _designSpriteGroupKey = null;
                public string DesignSpriteGroupKey
                {
                    get
                    {
                        if (_designSpriteGroupKey == null)
                        {
                            _designSpriteGroupKey = comm.objst.RSprite.DEFAULT_GROUP_KEY;
                        }
                        return _designSpriteGroupKey;
                    }
                    set { _designSpriteGroupKey = value; }
                }

                private Dictionary<string, lib.ObjEV> _EVDic = null;
                public Dictionary<string, lib.ObjEV> EVDic
                {
                    get
                    {
                        if (_EVDic == null)
                        {
                            _EVDic = new Dictionary<string, lib.ObjEV>();
                        }
                        return _EVDic;
                    }
                    set { _EVDic = value; }
                }

                public override List<int[]> GetPathList(DefineFile defineFile)
                {
                    try
                    {
                        var dobj = defineFile.GetObject(DObjectId);
                        if (dobj.PathSizeType == PathSizeType.PS_NONE)
                        {
                            var dobjData = defineFile.GetObjectData(DObjectId);
                            var rpack = comm.objst.RPack.FromFile(dobj.RPackFile);
                            var dirs = objst.DObject.Data.GetDirTypes();
                            var groupList = new List<objst.RSprite.Group>();
                            foreach (var acKey in dobjData.ActionDic.Keys)
                            {
                                var sprites = rpack.FindResource<comm.objst.RSprite>(DObjectId + "_" + acKey);
                                if (sprites.Length > 0)
                                {
                                    var sprite = sprites[0];
                                    groupList.AddRange(sprite.FindGroup(dirs));
                                }
                            }
                            return RSprite.Group.MergeAllGroupPath(groupList.ToArray());
                        }
                    }
                    catch (Exception)
                    {
                    }
                    return new List<int[]>();
                }
                public T FindEV<T>()
                {
                    return lib.ObjEVDefine.FindEV<T>(EVDic);
                }
                public T FindEV<T>(out string key)
                {
                    return lib.ObjEVDefine.FindEV<T>(EVDic, out key);
                }
            }
            #endregion

            public const string FILE_EXT_NAME = ".map2";
            public const int PATH_SIZE = 16;
            public const int IMAGE_BLOCK_SIZE = 512;

            public static string[] GetAllLayerTypes()
            {
                return EnumHelper.GetNames(typeof(comm.objst.MapLayerType));
            }
            public static string[] GetSpaceLayerTypes()
            {
                var list = new List<string>(GetAllLayerTypes());
                return list.ToArray();
            }
            public static string[] GetMapDesignLayerTypes()
            {
                var list = new List<string>(GetAllLayerTypes());
                list.Remove(comm.objst.MapLayerType.PATH.ToString());
                list.Insert(list.Count - 1, comm.objst.MapLayerType.PATH.ToString());
                return list.ToArray();
            }
            public static string[] GetSpriteDesignLayerTypes()
            {
                var list = new List<string>(GetAllLayerTypes());
                list.Remove(comm.objst.MapLayerType.BACKGROUND.ToString());
                list.Remove(comm.objst.MapLayerType.PATH.ToString());
                list.Add(comm.objst.MapLayerType.PATH.ToString());
                return list.ToArray();
            }
            public static string[] GetObjChooseLayerTypes()
            {
                var list = new List<string>(GetAllLayerTypes());
                list.Remove(comm.objst.MapLayerType.BACKGROUND.ToString());
                list.Remove(comm.objst.MapLayerType.PATH.ToString());
                list.Remove(comm.objst.MapLayerType.GRID.ToString());
                list.Remove(comm.objst.MapLayerType.DESIGN.ToString());
                return list.ToArray();
            }

            public int Width { get; set; }
            public int Height { get; set; }

            public int DesignX { get; set; }
            public int DesignY { get; set; }

            private double _designScale = 1;
            public double DesignScale
            {
                get { return _designScale; }
                set { _designScale = value; }
            }

            private List<ImageBlock> _bgImageBlockList = null;
            public List<ImageBlock> BGImageBlockList
            {
                get
                {
                    if (_bgImageBlockList == null)
                    {
                        _bgImageBlockList = new List<ImageBlock>();
                    }
                    return _bgImageBlockList;
                }
                set { _bgImageBlockList = value; }
            }

            private PathType[,] _paths = null;
            public PathType[,] Paths
            {
                get
                {
                    if (_paths == null)
                    {
                        _paths = new PathType[,] { };
                    }
                    return _paths;
                }
                set { _paths = value; }
            }

            private int _pathSize = PATH_SIZE;
            public int PathSize
            {
                get { return _pathSize; }
                set { _pathSize = value; }
            }

            private Dictionary<string, Obj> _objDic = null;
            public Dictionary<string, Obj> ObjDic
            {
                get
                {
                    if (_objDic == null)
                    {
                        _objDic = new Dictionary<string, Obj>();
                    }
                    return _objDic;
                }
                set { _objDic = value; }
            }

            public comm.objst.PathType GetPathType(int x, int y)
            {
                if (x < Width && y < Height)
                {
                    return _paths[x, y];
                }
                return comm.objst.PathType.UNDEFINED;
            }
            public void AddExObjDic(Dictionary<string, Obj> exObjDic)
            {
                foreach (var key in exObjDic.Keys)
                {
                    ObjDic.Add(key, exObjDic[key]);
                }
            }
            public Dictionary<string, Obj> RemoveExObjDic()
            {
                var delKeys = new List<string>();
                var eDic = new Dictionary<string, comm.objst.DMap.Data.Obj>();
                foreach (var key in ObjDic.Keys)
                {
                    if (ObjDic[key].IsExternal)
                    {
                        eDic.Add(key, ObjDic[key]);
                        delKeys.Add(key);
                    }
                }
                foreach (var key in delKeys)
                {
                    ObjDic.Remove(key);
                }
                return eDic;
            }
            public void FillAllObjPath(comm.DefineFile defineFile)
            {
                foreach (var mObj in ObjDic.Values)
                {
                    var dots = mObj.GetPathList(defineFile);
                    foreach (var dot in dots)
                    {
                        var type = GetPathType(dot[0] + mObj.LocX, dot[1] + mObj.LocY);
                        if (type == objst.PathType.EMPTY)
                        {
                            Paths[dot[0] + mObj.LocX, dot[1] + mObj.LocY] = objst.PathType.FORBID;
                        }
                    }
                }
            }
            public Dictionary<string, T> FindObj<T>()
            {
                var dic = new Dictionary<string, T>();
                foreach (var key in ObjDic.Keys)
                {
                    if (ObjDic[key] is T)
                    {
                        dic.Add(key, (T)(object)ObjDic[key]);
                    }
                }
                return dic;
            }
        }
        #endregion

        private string _file = null;
        [DisplayName("文件"), Category("文件引用"), Editor(typeof(editor.FileChooseEditor), typeof(UITypeEditor))]
        public string File
        {
            get
            {
                if (_file == null)
                {
                    _file = "";
                }
                return _file;
            }
            set { _file = value; }
        }

        private string _classType = null;
        [Editor(typeof(editor.ClassTypeEditor), typeof(UITypeEditor))]
        public string ClassType
        {
            get
            {
                if (_classType == null)
                {
                    _classType = "";
                }
                return _classType;
            }
            set { _classType = value; }
        }
    }
    #endregion

    #region DStar
    [Serializable]
    public class DStar : DDefine, IClassBind
    {
        #region Data
        [Serializable]
        public class Data
        {
            public const string FILE_EXT_NAME = ".star";

            #region Faction
            [Serializable]
            public class Faction
            {
                private string _name = null;
                public string Name
                {
                    get
                    {
                        if (_name == null)
                        {
                            _name = "";
                        }
                        return _name;
                    }
                    set { _name = value; }
                }

                private List<string> _baseSpaceIdList = null;
                public List<string> BaseSpaceIdList
                {
                    get
                    {
                        if (_baseSpaceIdList == null)
                        {
                            _baseSpaceIdList = new List<string>();
                        }
                        return _baseSpaceIdList;
                    }
                    set { _baseSpaceIdList = value; }
                }

                private Color _color = null;
                public Color Color
                {
                    get
                    {
                        if (_color == null)
                        {
                            _color = new Color();
                        }
                        return _color;
                    }
                    set { _color = value; }
                }

                public byte[] FlagImageBytes { get; set; }
            }
            #endregion

            #region Space
            [Serializable]
            public class Space
            {
                private string _spaceId = null;
                public string SpaceId
                {
                    get
                    {
                        if (_spaceId == null)
                        {
                            _spaceId = "";
                        }
                        return _spaceId;
                    }
                    set { _spaceId = value; }
                }

                public SpaceType SpaceType { get; set; }

                private string _mapId = null;
                public string MapId
                {
                    get
                    {
                        if (_mapId == null)
                        {
                            _mapId = "";
                        }
                        return _mapId;
                    }
                    set { _mapId = value; }
                }

                private List<int[]> _pathList = null;
                public List<int[]> PathList
                {
                    get
                    {
                        if (_pathList == null)
                        {
                            _pathList = new List<int[]>();
                        }
                        return _pathList;
                    }
                    set { _pathList = value; }
                }

                public int CentreX { get; set; }
                public int CentreY { get; set; }

                private Dictionary<string, DMap.Data.Obj> _objDic = null;
                public Dictionary<string, DMap.Data.Obj> ObjDic
                {
                    get
                    {
                        if (_objDic == null)
                        {
                            _objDic = new Dictionary<string, DMap.Data.Obj>();
                        }
                        return _objDic;
                    }
                    set { _objDic = value; }
                }
            }
            #endregion

            #region Area
            [Serializable]
            public class Area
            {
                public int DesignX { get; set; }
                public int DesignY { get; set; }
                public int MapBlockSize { get; set; }
                public int MapSize { get; set; }
                public int Width { get; set; }
                public int Height { get; set; }

                public byte[] BGImageBytes { get; set; }

                private Space[,] _spaces = null;
                public Space[,] Spaces
                {
                    get
                    {
                        if (_spaces == null)
                        {
                            _spaces = new Space[,] { };
                        }
                        return _spaces;
                    }
                    set { _spaces = value; }
                }

                public Space GetSpace(string spaceId)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        for (int y = 0; y < Height; y++)
                        {
                            if (Spaces[x, y].SpaceId == spaceId)
                            {
                                return Spaces[x, y];
                            }
                        }
                    }
                    return null;
                }
            }
            #endregion

            public static string[] GetSpaceTypes()
            {
                return comm.EnumHelper.GetNames(typeof(SpaceType));
            }

            public const string DEFAULT_AREA_KEY = "DEFAULT";

            public int SpaceStartId { get; set; }

            private Dictionary<string, Faction> _factionDic = null;
            public Dictionary<string, Faction> FactionDic
            {
                get
                {
                    if (_factionDic == null)
                    {
                        _factionDic = new Dictionary<string, Faction>();
                    }
                    return _factionDic;
                }
                set { _factionDic = value; }
            }

            private Dictionary<string, Area> _areaDic = null;
            public Dictionary<string, Area> AreaDic
            {
                get
                {
                    if (_areaDic == null)
                    {
                        _areaDic = new Dictionary<string, Area> { { DEFAULT_AREA_KEY, new Area() } };
                    }
                    return _areaDic;
                }
                set { _areaDic = value; }
            }

            public Space GetSpace(string spaceId)
            {
                foreach (var area in AreaDic.Values)
                {
                    var space = area.GetSpace(spaceId);
                    if (space != null)
                    {
                        return space;
                    }
                }
                return null;
            }
        }
        #endregion

        [DisplayName("玩家可以创建英雄")]
        public bool CanCreateHeroByPlayer { get; set; }

        private string _file = null;
        [DisplayName("文件"), Category("文件引用"), Editor(typeof(editor.FileChooseEditor), typeof(UITypeEditor))]
        public string File
        {
            get
            {
                if (_file == null)
                {
                    _file = "";
                }
                return _file;
            }
            set { _file = value; }
        }

        [DisplayName("空间开始Id")]
        public int SpaceStartId { get; set; }

        [DisplayName("阵营开始Id")]
        public int FactionStartId { get; set; }

        private string _classType = null;
        [Editor(typeof(editor.ClassTypeEditor), typeof(UITypeEditor))]
        public string ClassType
        {
            get
            {
                if (_classType == null)
                {
                    _classType = "";
                }
                return _classType;
            }
            set { _classType = value; }
        }


        public string ToRawId(string id)
        {
            double cc = 1000;
            var numId = int.Parse(id);
            return (numId - Math.Floor(numId / cc) * cc).ToString();
        }
        public string ToRealFactionId(string factionId)
        {
            return (int.Parse(factionId) + FactionStartId).ToString();
        }
        public string ToRealSpaceId(string spaceId)
        {
            return (int.Parse(spaceId) + SpaceStartId).ToString();
        }
    }
    #endregion
}
